//
//  Second1ViewController.m
//  UI_豆瓣项目
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "Second1ViewController.h"
#import "Second1Model.h"
#import "SecondCollectionViewCell.h"
#import "UIImageView+WebCache.h"
@interface Second1ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)NSMutableArray *arrModel;
@property(nonatomic, retain)UICollectionView *view1;

@end

@implementation Second1ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 集合视图
    self.view.backgroundColor = [UIColor yellowColor];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 垂直视图
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake((375 - 40) / 3, 200);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.view1 = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    [self.view addSubview:_view1];
    _view1.backgroundColor = [UIColor whiteColor];
    _view1.delegate = self;
    _view1.dataSource = self;
    [_view1 release];
    // 重用池
    [_view1 registerNib:[UINib nibWithNibName:@"SecondCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cell"];
    // 数据解析
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
//    NSData *data = [NSData dataWithContentsOfFile:path];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    NSLog(@"%@", arr);
//    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//    NSLog(@"%@", result);
//    [_view1 registerNib:[UINib nibWithNibName:@"SecondCollectionViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    
    self.arrModel = [NSMutableArray array];
    for (NSMutableDictionary *dic in arr) {
        Second1Model *model = [Second1Model second1Model];
        [model setValuesForKeysWithDictionary:dic];
        [_arrModel addObject:model];
    }
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _arrModel.count;
}

- (SecondCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//    UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"a3.png"]];
//    cell.backgroundView = view;

    Second1Model *model = _arrModel[indexPath.row];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.large]];
    cell.myLabel.text = model.name;
    
    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
